package com.pur.zsms.accountmanage.web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.accountmanage.service.IAccountService;
import com.pur.zsms.accountmanage.vo.AccountDayDetailVo;
import com.pur.zsms.accountmanage.vo.AccountDayVo;
import com.pur.zsms.accountmanage.vo.AccountFooterVo;
import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.accountmanage.vo.StoreInAccountDetailedVo;
import com.pur.zsms.accountmanage.vo.StoreInAccountVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.foodmanage.food.vo.FoodMaterVo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderCustFoodVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.storemanage.po.StoreInDetailedPo;
import com.pur.zsms.storemanage.po.StoreOutDetailedPo;
import com.pur.zsms.storemanage.service.IStoreService;
import com.pur.zsms.storemanage.vo.StoreInDetailedVo;
import com.pur.zsms.storemanage.vo.StoreInVo;
import com.pur.zsms.storemanage.vo.StoreMaterVo;
import com.pur.zsms.storemanage.vo.StoreOutDetailedVo;
import com.pur.zsms.storemanage.vo.StoreOutVo;
import com.pur.zsms.storemanage.vo.StoreVo;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.service.ISupplierService;

@Controller
@RequestMapping(value = "/account")
public class AccountControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(AccountControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "accountService")
	private IAccountService acountService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "userService")
	private IUserService userService;

	@RequestMapping(value = "/findStoreInAccounts.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findStoreInDetailed(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, AccountVo accountVo, HttpSession session,
	        String storeInId) throws GenericException {

		JSONObject json = new JSONObject();
		if (accountVo.getSelectFlag() == 0) {
			json.put("success", true);
			log.info("----" + json.toString());
			return json.toString();
		}
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = acountService.listAccountsDetialed(pageController, accountVo);

		List<AccountFooterVo> accountVoListFooter = new ArrayList<AccountFooterVo>();
		AccountFooterVo tAccountVoFooter = new AccountFooterVo();
		BigDecimal materValueAll = new BigDecimal(0);

		List<AccountVo> accountVoList = new ArrayList<AccountVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				AccountVo f = new AccountVo();
				f.setId((Integer) item[0]);
				f.setStoreInId((Integer) item[1]);
				f.setStoreHouseId((Integer) item[2]);
				f.setStoreName((String) item[3]);
				f.setCustId((Integer) item[4]);
				f.setCustName((String) item[5]);
				f.setSupId((Integer) item[6]);
				f.setSupName((String) item[7]);
				f.setMaterId((Integer) item[8]);
				f.setMaterName((String) item[9]);
				f.setMaterLevel((Integer) item[10]);
				f.setStoreInWay((Integer) item[11]);
				f.setStoreInState((Integer) item[12]);
				f.setCreatDate((String) item[13]);
				f.setMaterNum((Integer) item[14]);
				f.setMaterValue((BigDecimal) item[15]);
				f.setAccountState((Integer) item[16]);
				f.setAccountDate((String) item[17]);
				materValueAll = materValueAll.add((BigDecimal) item[15]);
				accountVoList.add(f);
			}
		}

		tAccountVoFooter.setMaterValue(materValueAll);
		tAccountVoFooter.setStoreInId("结款合计");
		accountVoListFooter.add(tAccountVoFooter);

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", accountVoList);
		json.put("footer", accountVoListFooter);

		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findAccountsDetailed.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findAccountsDetailed(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, AccountVo accountVo, HttpSession session,
	        String accountId) throws GenericException {

		JSONObject json = new JSONObject();

		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		accountVo.setAccountId(Integer.valueOf(accountId));

		this.pageList = acountService.listAccountsDetialedSelect(pageController, accountVo);

		List<AccountFooterVo> accountVoListFooter = new ArrayList<AccountFooterVo>();
		AccountFooterVo tAccountVoFooter = new AccountFooterVo();
		BigDecimal materValueAll = new BigDecimal(0);

		List<AccountVo> accountVoList = new ArrayList<AccountVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				AccountVo f = new AccountVo();
				f.setId((Integer) item[0]);
				f.setStoreInId((Integer) item[1]);
				f.setStoreHouseId((Integer) item[2]);
				f.setStoreName((String) item[3]);
				f.setCustId((Integer) item[4]);
				f.setCustName((String) item[5]);
				f.setSupId((Integer) item[6]);
				f.setSupName((String) item[7]);
				f.setMaterId((Integer) item[8]);
				f.setMaterName((String) item[9]);
				f.setMaterLevel((Integer) item[10]);
				f.setStoreInWay((Integer) item[11]);
				f.setStoreInState((Integer) item[12]);
				f.setCreatDate((String) item[13]);
				f.setMaterNum((Integer) item[14]);
				f.setMaterValue((BigDecimal) item[15]);
				f.setAccountState((Integer) item[16]);
				f.setAccountDate((String) item[17]);
				materValueAll = materValueAll.add((BigDecimal) item[15]);
				accountVoList.add(f);
			}
		}

		tAccountVoFooter.setMaterValue(materValueAll);
		tAccountVoFooter.setStoreInId("结款合计");
		accountVoListFooter.add(tAccountVoFooter);

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", accountVoList);
		json.put("footer", accountVoListFooter);

		log.info("----" + json.toString());
		return json.toString();
	}

	// 导出excel方法
	@RequestMapping(value = "/exportExcel.do", method = { RequestMethod.POST })
	public void exportExcel(HttpServletRequest request, HttpServletResponse response, AccountVo accountVo) {

		HttpSession session = request.getSession();
		session.setAttribute("state", null);
		// 生成提示信息，
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("account" + PubFun.getCurrentDate(), "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
			// response.addHeader("Content-Disposition",
			// "attachment;   filename=" + codedFileName + ".xls");
			// 产生工作簿对象
			HSSFWorkbook workbook = new HSSFWorkbook();
			// 产生工作表对象
			HSSFSheet sheet = workbook.createSheet();
			sheet.setColumnWidth(0, 2000);
			sheet.setColumnWidth(1, 3000);
			sheet.setColumnWidth(4, 3000);
			sheet.setColumnWidth(5, 3000);
			sheet.setColumnWidth(13, 3000);
			sheet.setColumnWidth(14, 5500);
			sheet.setColumnWidth(17, 3000);

			// 边框
			HSSFCellStyle setBorder = workbook.createCellStyle();
			setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
			setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
			setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
			setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

			// 初始行设置
			HSSFRow rowfist = sheet.createRow((int) 0);// 创建一行

			HSSFCell cellFirst1 = rowfist.createCell((int) 0);// 创建一列
			cellFirst1.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst1.setCellValue("ID");
			cellFirst1.setCellStyle(setBorder);

			HSSFCell cellFirst2 = rowfist.createCell((int) 1);// 创建一列
			cellFirst2.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst2.setCellValue("入库单号");
			cellFirst2.setCellStyle(setBorder);

			HSSFCell cellFirst3 = rowfist.createCell((int) 2);// 创建一列
			cellFirst3.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst3.setCellValue("仓库号");
			cellFirst3.setCellStyle(setBorder);

			HSSFCell cellFirst4 = rowfist.createCell((int) 3);// 创建一列
			cellFirst4.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst4.setCellValue("仓库名");
			cellFirst4.setCellStyle(setBorder);

			HSSFCell cellFirst5 = rowfist.createCell((int) 4);// 创建一列
			cellFirst5.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst5.setCellValue("仓库管理人id");
			cellFirst5.setCellStyle(setBorder);

			HSSFCell cellFirst6 = rowfist.createCell((int) 5);// 创建一列
			cellFirst6.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst6.setCellValue("仓库管理人名");
			cellFirst6.setCellStyle(setBorder);

			HSSFCell cellFirst7 = rowfist.createCell((int) 6);// 创建一列
			cellFirst7.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst7.setCellValue("供货商id");
			cellFirst7.setCellStyle(setBorder);

			HSSFCell cellFirst8 = rowfist.createCell((int) 7);// 创建一列
			cellFirst8.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst8.setCellValue("供货商名");
			cellFirst8.setCellStyle(setBorder);

			HSSFCell cellFirst9 = rowfist.createCell((int) 8);// 创建一列
			cellFirst9.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst9.setCellValue("材料id");
			cellFirst9.setCellStyle(setBorder);

			HSSFCell cellFirst10 = rowfist.createCell((int) 9);// 创建一列
			cellFirst10.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst10.setCellValue("材料名");
			cellFirst10.setCellStyle(setBorder);

			HSSFCell cellFirst11 = rowfist.createCell((int) 10);// 创建一列
			cellFirst11.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst11.setCellValue("品级");
			cellFirst11.setCellStyle(setBorder);

			HSSFCell cellFirst12 = rowfist.createCell((int) 11);// 创建一列
			cellFirst12.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst12.setCellValue("入库方式");
			cellFirst12.setCellStyle(setBorder);

			HSSFCell cellFirst13 = rowfist.createCell((int) 12);// 创建一列
			cellFirst13.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst13.setCellValue("入库状态");
			cellFirst13.setCellStyle(setBorder);

			HSSFCell cellFirst14 = rowfist.createCell((int) 13);// 创建一列
			cellFirst14.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst14.setCellValue("入库日");
			cellFirst14.setCellStyle(setBorder);

			HSSFCell cellFirst15 = rowfist.createCell((int) 14);// 创建一列
			cellFirst15.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst15.setCellValue("实际入库数量(公斤/件/个)");
			cellFirst15.setCellStyle(setBorder);

			HSSFCell cellFirst16 = rowfist.createCell((int) 15);// 创建一列
			cellFirst16.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst16.setCellValue("结款价钱");
			cellFirst16.setCellStyle(setBorder);

			HSSFCell cellFirst17 = rowfist.createCell((int) 16);// 创建一列
			cellFirst17.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst17.setCellValue("结款状态");
			cellFirst17.setCellStyle(setBorder);

			HSSFCell cellFirst18 = rowfist.createCell((int) 17);// 创建一列
			cellFirst18.setCellType(HSSFCell.CELL_TYPE_STRING);
			cellFirst18.setCellValue("结款日");
			cellFirst18.setCellStyle(setBorder);

			// 中间行设置
			JSONArray array = JSONArray.fromObject(accountVo.getExcelList());
			// 合计价钱
			BigDecimal materValueAll = new BigDecimal(0);
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));

				// 累计合计价钱---下
				materValueAll = materValueAll.add(new BigDecimal(object.get("materValue").toString()));

				// 累计合计价钱---上

				HSSFRow row = sheet.createRow((int) i + 1);// 创建一行

				HSSFCell cell1 = row.createCell((int) 0);// 创建一列
				cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell1.setCellValue(object.get("id").toString());
				cell1.setCellStyle(setBorder);

				HSSFCell cell2 = row.createCell((int) 1);// 创建一列
				cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell2.setCellValue(object.get("storeInId").toString());
				cell2.setCellStyle(setBorder);

				HSSFCell cell3 = row.createCell((int) 2);// 创建一列
				cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell3.setCellValue(object.get("storeHouseId").toString());
				cell3.setCellStyle(setBorder);

				HSSFCell cell4 = row.createCell((int) 3);// 创建一列
				cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell4.setCellValue(object.get("storeName").toString());
				cell4.setCellStyle(setBorder);

				HSSFCell cell5 = row.createCell((int) 4);// 创建一列
				cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell5.setCellValue(object.get("custId").toString());
				cell5.setCellStyle(setBorder);

				HSSFCell cell6 = row.createCell((int) 5);// 创建一列
				cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell6.setCellValue(object.get("custName").toString());
				cell6.setCellStyle(setBorder);

				HSSFCell cell7 = row.createCell((int) 6);// 创建一列
				cell7.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell7.setCellValue(object.get("supId").toString());
				cell7.setCellStyle(setBorder);

				HSSFCell cell8 = row.createCell((int) 7);// 创建一列
				cell8.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell8.setCellValue(object.get("supName").toString());
				cell8.setCellStyle(setBorder);

				HSSFCell cell9 = row.createCell((int) 8);// 创建一列
				cell9.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell9.setCellValue(object.get("materId").toString());
				cell9.setCellStyle(setBorder);

				HSSFCell cell10 = row.createCell((int) 9);// 创建一列
				cell10.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell10.setCellValue(object.get("materName").toString());
				cell10.setCellStyle(setBorder);

				HSSFCell cell11 = row.createCell((int) 10);// 创建一列
				cell11.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell11.setCellValue(object.get("materLevel").toString());
				cell11.setCellStyle(setBorder);

				HSSFCell cell12 = row.createCell((int) 11);// 创建一列
				cell12.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell12.setCellValue(object.get("storeInWay").toString());
				cell12.setCellStyle(setBorder);

				HSSFCell cell13 = row.createCell((int) 12);// 创建一列
				cell13.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell13.setCellValue(object.get("storeInState").toString());
				cell13.setCellStyle(setBorder);

				HSSFCell cell14 = row.createCell((int) 13);// 创建一列
				cell14.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell14.setCellValue(object.get("creatDate").toString());
				cell14.setCellStyle(setBorder);

				HSSFCell cell15 = row.createCell((int) 14);// 创建一列
				cell15.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell15.setCellValue(object.get("materNum").toString());
				cell15.setCellStyle(setBorder);

				HSSFCell cell16 = row.createCell((int) 15);// 创建一列
				cell16.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell16.setCellValue(object.get("materValue").toString());
				cell16.setCellStyle(setBorder);

				HSSFCell cell17 = row.createCell((int) 16);// 创建一列
				cell17.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell17.setCellValue(object.get("accountState").toString());
				cell17.setCellStyle(setBorder);

				HSSFCell cell18 = row.createCell((int) 17);// 创建一列
				cell18.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell18.setCellValue(object.get("accountDate").toString());
				cell18.setCellStyle(setBorder);

			}

			// 结尾合计设置
			// 初始行设置
			HSSFRow rowlast = sheet.createRow(array.size() + 1);// 创建一行

			HSSFCell celllast1 = rowlast.createCell((int) 0);// 创建一列
			celllast1.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast1.setCellValue("结款合计");
			celllast1.setCellStyle(setBorder);

			HSSFCell celllast2 = rowlast.createCell((int) 1);// 创建一列
			celllast2.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast2.setCellValue("");
			celllast2.setCellStyle(setBorder);

			HSSFCell celllast3 = rowlast.createCell((int) 2);// 创建一列
			celllast3.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast3.setCellValue("");
			celllast3.setCellStyle(setBorder);

			HSSFCell celllast4 = rowlast.createCell((int) 3);// 创建一列
			celllast4.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast4.setCellValue("");
			celllast4.setCellStyle(setBorder);

			HSSFCell celllast5 = rowlast.createCell((int) 4);// 创建一列
			celllast5.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast5.setCellValue("");
			celllast5.setCellStyle(setBorder);

			HSSFCell celllast6 = rowlast.createCell((int) 5);// 创建一列
			celllast6.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast6.setCellValue("");
			celllast6.setCellStyle(setBorder);

			HSSFCell celllast7 = rowlast.createCell((int) 6);// 创建一列
			celllast7.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast7.setCellValue("");
			celllast7.setCellStyle(setBorder);

			HSSFCell celllast8 = rowlast.createCell((int) 7);// 创建一列
			celllast8.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast8.setCellValue("");
			celllast8.setCellStyle(setBorder);

			HSSFCell celllast9 = rowlast.createCell((int) 8);// 创建一列
			celllast9.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast9.setCellValue("");
			celllast9.setCellStyle(setBorder);

			HSSFCell celllast10 = rowlast.createCell((int) 9);// 创建一列
			celllast10.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast10.setCellValue("");
			celllast10.setCellStyle(setBorder);

			HSSFCell celllast11 = rowlast.createCell((int) 10);// 创建一列
			celllast11.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast11.setCellValue("");
			celllast11.setCellStyle(setBorder);

			HSSFCell celllast12 = rowlast.createCell((int) 11);// 创建一列
			celllast12.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast12.setCellValue("");
			celllast12.setCellStyle(setBorder);

			HSSFCell celllast13 = rowlast.createCell((int) 12);// 创建一列
			celllast13.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast13.setCellValue("");
			celllast13.setCellStyle(setBorder);

			HSSFCell celllast14 = rowlast.createCell((int) 13);// 创建一列
			celllast14.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast14.setCellValue("");
			celllast14.setCellStyle(setBorder);

			HSSFCell celllast15 = rowlast.createCell((int) 14);// 创建一列
			celllast15.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast15.setCellValue("");
			celllast15.setCellStyle(setBorder);

			HSSFCell celllast16 = rowlast.createCell((int) 15);// 总共价钱
			celllast16.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast16.setCellValue(materValueAll.toString());
			celllast16.setCellStyle(setBorder);

			HSSFCell celllast17 = rowlast.createCell((int) 16);// 创建一列
			celllast17.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast17.setCellValue("");
			celllast17.setCellStyle(setBorder);

			HSSFCell celllast18 = rowlast.createCell((int) 17);// 创建一列
			celllast18.setCellType(HSSFCell.CELL_TYPE_STRING);
			celllast18.setCellValue("");
			celllast18.setCellStyle(setBorder);

			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
	}

	// 导出excel方法
	@RequestMapping(value = "/exportExcelOne.do", method = { RequestMethod.POST })
	public void exportExcelOne(HttpServletRequest request, HttpServletResponse response, AccountVo accountVo) {
		HttpSession session = request.getSession();
		session.setAttribute("state", null);
		// 生成提示信息，
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode("1234", "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
			// response.addHeader("Content-Disposition",
			// "attachment;   filename=" + codedFileName + ".xls");
			// 产生工作簿对象
			HSSFWorkbook workbook = new HSSFWorkbook();
			// 产生工作表对象
			HSSFSheet sheet = workbook.createSheet();
			for (int i = 0; i <= 30000; i++) {
				HSSFRow row = sheet.createRow((int) i);// 创建一行
				HSSFCell cell = row.createCell((int) 0);// 创建一列
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue("测试成功" + i);
			}
			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (UnsupportedEncodingException e1) {
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
			}
			session.setAttribute("state", "open");
		}
		System.out.println("文件生成...");
	}

	@RequestMapping("check")
	public void check(HttpServletRequest request, HttpServletResponse response) {
		try {
			if ("open".equals(request.getSession().getAttribute("state"))) {
				request.getSession().setAttribute("state", null);
				response.getWriter().write("true");
				response.getWriter().flush();
			} else {
				response.getWriter().write("false");
				response.getWriter().flush();
			}
		} catch (IOException e) {
		}
	}

	@RequestMapping(value = "/findAccountProcess.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findAccountProcess(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, StoreInAccountVo storeInAccountVo,
	        HttpSession session, String storeInId) throws GenericException {

		JSONObject json = new JSONObject();

		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = acountService.listAccountProcess(pageController, storeInAccountVo);

		BigDecimal materValueAll = new BigDecimal(0);
		List<StoreInAccountVo> storeInAccountVoList = new ArrayList<StoreInAccountVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				StoreInAccountVo f = new StoreInAccountVo();

				f.setAccountId((Integer) item[0]);
				f.setAccountDate((String) item[1]);
				f.setMaterValueAll((BigDecimal) item[2]);
				f.setRemark((String) item[3]);
				f.setSupId((Integer) item[4]);
				f.setSupName((String) item[5]);

				f.setContent("查看详细结款单");
				storeInAccountVoList.add(f);
			}
		}

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeInAccountVoList);

		log.info("----" + json.toString());
		return json.toString();
	}

	// 结款
	@RequestMapping(value = "/commitAccount.do", method = { RequestMethod.POST })
	@ResponseBody
	public String commitAccount(StoreInAccountDetailedVo storeInAccountDetailedVo) {
		JSONObject json = new JSONObject();

		try {
			/*
			 * //处理前台传入json数据 --页脚合计 JSONArray arrayFoot =
			 * JSONArray.fromObject(storeInAccountDetailedVo.getFootList());
			 * 
			 * BigDecimal materValueAll = new BigDecimal(0); for (int i = 0; i <
			 * arrayFoot.size(); i++) { JSONObject object =
			 * JSONObject.fromObject(arrayFoot.get(i)); materValueAll =new
			 * BigDecimal(object.get("materValue").toString()); }
			 * 
			 * //处理前台传入json数据 --关联id JSONArray array =
			 * JSONArray.fromObject(storeInAccountDetailedVo.getIdList());
			 * List<StoreInAccountDetailedVo> storeInAccountDetailedVoList = new
			 * ArrayList<StoreInAccountDetailedVo>(); for (int i = 0; i <
			 * array.size(); i++) { JSONObject object =
			 * JSONObject.fromObject(array.get(i)); StoreInAccountDetailedVo
			 * tStoreInAccountDetailedVo = new StoreInAccountDetailedVo();
			 * tStoreInAccountDetailedVo
			 * .setId(Integer.valueOf(object.get("id").toString()));
			 * storeInAccountDetailedVoList.add(tStoreInAccountDetailedVo); }
			 */
			acountService.commitAccount(storeInAccountDetailedVo);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	/**
	 * 进入客户详细订单
	 * */
	@RequestMapping(value = "/goAccountDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goAccountDetailed(String accountId) {
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		map.put("accountId", accountId);
		return new ModelAndView("/accounts/accountsDetailed", map);
	}

	/**
	 * 客户日报表
	 * */
	@RequestMapping(value = "/accountListForCust.do", method = { RequestMethod.POST })
	@ResponseBody
	public String accountListForCust(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, AccountDayVo accountDayVo, HttpSession session,
	        String storeInId) throws GenericException {

		JSONObject json = new JSONObject();

		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		List<AccountDayVo> accountDayVoList = new ArrayList<AccountDayVo>();
		if (accountDayVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			json.put("rows", accountDayVoList);
			return json.toString();
		}

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = acountService.accountListForCust(pageController, accountDayVo);

		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				AccountDayVo f = new AccountDayVo();
				f.setCostValueAll((BigDecimal) item[0]);
				f.setOrdDate((String) item[1]);
				f.setCustId((Integer) item[2]);
				f.setCustName((String) item[3]);
				f.setTurnValueAll((BigDecimal) item[4]);
				if (item[5] == null) {
					f.setGrossProfitRate("暂无");
				} else {
					f.setGrossProfitRate((String) item[5] + "%");
				}
				f.setContent("查看详细内容");
				accountDayVoList.add(f);
			}
		}

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", accountDayVoList);

		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 进入日核算详细页面
	 * */
	@RequestMapping(value = "/goAccountListForCustDetail.do", method = { RequestMethod.GET })
	public ModelAndView goAccountListForCustDetail(AccountDayVo accountDayVo) {
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		try {
			String realname = userService.getUserVoById(String.valueOf(accountDayVo.getCustId()))
			        .getRealName();
			accountDayVo.setCustName(realname);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		map.put("accountDayVo", accountDayVo);
		return new ModelAndView("/accounts/accountsListForCustDetail", map);
	}

	/**
	 * 客户日报表
	 * */
	@RequestMapping(value = "/accountListForCustDetailForOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String accountListForCustDetailForOrder(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, AccountDayDetailVo accountDayDetailVo,
	        HttpSession session, String storeInId) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";

		UserPo userInfo = (UserPo) session.getAttribute("user");
		accountDayDetailVo.setCustId(userInfo.getUserId());

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = acountService.accountListForCustDetailForOrder(pageController, accountDayDetailVo);

		List<AccountDayDetailVo> accountDayVoList = new ArrayList<AccountDayDetailVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				AccountDayDetailVo f = new AccountDayDetailVo();
				f.setOrdId((Integer) item[0]);
				f.setCustId(Long.valueOf(item[1].toString()));
				f.setCustName((String) item[2]);
				f.setOrdDate((String) item[3]);
				f.setMaterId((Integer) item[4]);
				f.setMaterName((String) item[5]);
				f.setSpecifications((String) item[6]);
				f.setBrands((String) item[7]);
				f.setMaterWeight((BigDecimal) item[8]);
				f.setUnit((String) item[9]);
				f.setMaterValue((BigDecimal) item[10]);
				f.setSupId((Integer) item[11]);
				f.setSupName((String) item[12]);
				f.setReturnGoods((Integer) item[13]);
				accountDayVoList.add(f);
			}
		}

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", accountDayVoList);

		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 
	 * 入库详细清单显示
	 * 
	 * */
	@RequestMapping(value = "/accountListForCustDetailForStoreOut.do", method = { RequestMethod.POST })
	@ResponseBody
	public String accountListForCustDetailForStoreOut(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, AccountDayDetailVo accountDayDetailVo,
	        HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = acountService.accountListForCustDetailForStoreOut(pageController, accountDayDetailVo);
		List<AccountDayDetailVo> storeOutDetailedVoList = new ArrayList<AccountDayDetailVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				AccountDayDetailVo f = new AccountDayDetailVo();
				f.setMaterId((Integer) item[0]);
				f.setMaterNumForOut(new BigDecimal(item[1].toString()));
				f.setMaterValueForOut(new BigDecimal(item[2].toString()));
				f.setMaterValueAllForOut(new BigDecimal(item[3].toString()));
				f.setRemark((String) item[4]);
				f.setMaterName((String) item[5]);
				f.setStoreOutId((Integer) item[6]);

				storeOutDetailedVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", storeOutDetailedVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 月报表
	 * */
	@RequestMapping(value = "/accountListForCustMonth.do", method = { RequestMethod.POST })
	@ResponseBody
	public String accountListForCustMonth(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, AccountDayVo accountDayVo, HttpSession session,
	        String storeInId) throws GenericException {

		JSONObject json = new JSONObject();

		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		List<AccountDayVo> accountDayVoList = new ArrayList<AccountDayVo>();
		if (accountDayVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			json.put("rows", accountDayVoList);
			return json.toString();
		}

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = acountService.accountListForCustMonth(pageController, accountDayVo);

		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				AccountDayVo f = new AccountDayVo();
				f.setCostValueAll((BigDecimal) item[0]);
				f.setOrdDateMonth((String) item[1]);
				f.setCustId((Integer) item[2]);
				f.setCustName((String) item[3]);
				f.setTurnValueAll((BigDecimal) item[4]);
				if (item[5] == null) {
					f.setGrossProfitRate("暂无");
				} else {
					f.setGrossProfitRate((String) item[5] + "%");
				}
				f.setContent("查看详细内容");
				accountDayVoList.add(f);
			}
		}

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", accountDayVoList);

		log.info("----" + json.toString());
		return json.toString();
	}
	
	/**
	 * 进入日核算详细页面
	 * */
	@RequestMapping(value = "/goAccountsListForManagerMonthDetail.do", method = { RequestMethod.GET })
	public ModelAndView accountsListForManagerMonthDetail(AccountDayVo accountDayVo) {
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		try {
			String realname = userService.getUserVoById(String.valueOf(accountDayVo.getCustId()))
			        .getRealName();
			accountDayVo.setCustName(realname);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		map.put("accountDayVo", accountDayVo);
		return new ModelAndView("/accounts/accountsListForManagerMonthDetail", map);
	}

}
